package cn.itcast.bos.web.action.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.service.base.CourierService;

@Actions
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class CourierAction extends ActionSupport implements ModelDriven<Courier> {
	//模型驱动注入
	private Courier  courier=new Courier();
	//service层的注入
	@Autowired
	private CourierService courierService;
	
	private int page;
	private int rows;
	public void setPage(int page) {
		this.page = page;//总页数
	}
	public void setRows(int rows) {
		this.rows = rows;//给页所有数据的集合
	}

	@Override
	public Courier getModel() {
		return courier;
	}

	
	@Action(value="courier_save",results={
			@Result(name="success",location="./pages/base/courier.html",type="redirect")
	})
	public String courier_save(){
		//执行操作的方法
		courierService.save(courier);
		return SUCCESS;
	}
	
	//分页查询所有的方法
	@Action(value="courier_pageQuery",results={
			@Result(name="success",type="json")
	})
	public String pageQuery(){
		Pageable pageable=new PageRequest(page-1, rows);
		
		Specification<Courier> specification =new Specification<Courier>() {

			@Override
				/*构造条件查询方法,如果返回值是null,代表无条件查询
				 * Root参数,获取条件表达式 name=?,age=?
				 * CriteriaQuery参数 构造简单条件查询,提供where方法
				 * CriteriaBuilder参数,构造Prodicate对象,条件查询,构造复杂条件查询效果
				 * */
			public Predicate toPredicate(Root<Courier> root, 
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				
				List<Predicate> list=new ArrayList<Predicate>();
				//当前查询Root根对象
				//单表查询,查询当前对象对应数据表
				if(StringUtils.isNotBlank(courier.getCourierNum())){
					//快递员工号查询
					//如:courNum=?
					Predicate p1 = cb.equal(root.get("courierNum").as(String.class),
							courier.getCourierNum());
					list.add(p1);
				}
				if(StringUtils.isNoneBlank(courier.getCompany())){
					//快递公司,模糊查询
					Predicate p2 = cb.like(root.get("compony").as(String.class),
							"%"+courier.getCompany()+"%");
					list.add(p2);
				}
				if(StringUtils.isNotBlank(courier.getType())){
					//快递员种类 精确查询
					Predicate p3 = cb.equal(root.get("type").as(String.class),
							courier.getType());
					list.add(p3);
				}
				
				//多表查询标准种类   迷糊查询
				Join<Object, Object> standardRoot = root.join("standard",JoinType.INNER);
				
				if(courier.getStandard()!=null && 
				   StringUtils.isNotBlank(courier.getStandard().getName())){
					Predicate p4 = cb.like(standardRoot.get("name").as(String.class),
							courier.getStandard().getName());
					
					list.add(p4);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		Page<Courier> page=courierService.findPageData(specification,pageable);
		Map<String,Object> result=new HashMap<String,Object>();
		result.put("page", page.getTotalElements());
		result.put("rows", page.getContent());
		
		ActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
	}
	
	//注入一个String的ids
	private String ids;
	
	public void setIds(String ids) {
		this.ids = ids;
	}
	//删除快递员的操作
	@Action(value="courier_delBatch",results={
			@Result(name="success",location="./pages/base/courier.html",type="redirect")
	})
	public String courier_delBatch(){
		System.out.println(ids);
		String[] idArray = ids.split(",");
		courierService.delBatch(idArray);
		return SUCCESS;
	}
	
	//为定区安排快递员
	@Action(value="courier_findnoassociation",results={
			@Result(name="success",type="json")
		})
	public String findnoassociation(){
		List<Courier> list=courierService.findnoassociation();
		//将返回获得的数据压住值栈中
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
	
	
	
	
}
